<template>
  <div id='detail'>

    <!-- 导航栏 -->
    <DetailNavBar class="detail-nav" @titleClick='titleClick' ref='navbar'></DetailNavBar>

    <Scroll class="content" ref='scroll' @scroll='contentScroll' :probeType='3'>
      <!-- 轮播图 -->
      <DetailSwiper :topImages='topImages'></DetailSwiper>
      <!-- 商品信息 -->
      <DetailBaseInfo :goods='goods'></DetailBaseInfo>
      <!-- 店铺信息 -->
      <DetailShopInfo :shop='shop'></DetailShopInfo>
      <!-- 商品详细图片列表 -->
      <DetailGoodsInfo :detailInfo='detailInfo' @imageLoad='imageLoad'></DetailGoodsInfo>
      <!-- 参数列表组件 -->
      <DetailParamInfo :paramInfo='paramInfo' ref='params'></DetailParamInfo>
      <!-- 用户评论信息展示 -->
      <DetailCommentInfo :commentInfo='commentInfo' ref='comment'></DetailCommentInfo>
      <!-- 推荐数据展示 -->
      <GoodsList :goods='recommends' ref="recommend"></GoodsList>
    </Scroll>
    <DetailBottomBar @addCart='addToCart'></DetailBottomBar>
    <BackTop v-show='isShowBackTop' @click.native='backTop'></BackTop>

    <!-- <Toast :message='message' :show='show'></Toast> -->

  </div>
</template>

<script>

  import DetailNavBar from './childComps/DetailNavBar'
  import DetailSwiper from './childComps/DetailSwiper'
  import DetailBaseInfo from './childComps/DetailBaseInfo'
  import DetailShopInfo from './childComps/DetailShopInfo'
  import DetailGoodsInfo from './childComps/DetailGoodsInfo'
  import DetailParamInfo from './childComps/DetailParamInfo'
  import DetailCommentInfo from './childComps/DetailCommentInfo'
  import DetailBottomBar from './childComps/DetailBottomBar'


  import Scroll from 'components/common/scroll/Scroll'
  import GoodsList from 'components/content/goods/GoodsList'
  // import Toast from 'components/common/toast/Toast'

  import { getDetail, Goods, Shop, GoodsParam, getRecommend } from 'network/detail.js'

  import { debounce } from '@/common/utils.js'
  import { itemListenerMixin, backTopMixin } from '@/common/mixin.js'

  export default {
    name: 'Detail',
    mixins: [itemListenerMixin, backTopMixin],
    data() {
      return {
        iid: null,
        topImages: [],
        goods: {},
        shop: {},
        detailInfo: {},
        paramInfo: {},
        commentInfo: {},
        recommends: [],
        themeTopYs: [],
        // message: '',
        // show: false
      }
    },
    created() {
      this.iid = this.$route.params.iid

      /* 
        请求详情数据
      */
      getDetail(this.iid).then(res => {
        // console.log(res);
        const data = res.result
        // 获取轮播图
        this.topImages = res.result.itemInfo.topImages
        // 获取商品信息
        this.goods = new Goods(data.itemInfo, data.columns, data.shopInfo.services)
        // console.log(this.goods);
        //店铺信息的对象
        this.shop = new Shop(data.shopInfo)
        // 保存商品的详情数据
        this.detailInfo = data.detailInfo
        // 获取参数信息
        this.paramInfo = new GoodsParam(data.itemParams.info, data.itemParams.rule)
        // 取出评论信息
        if (data.rate.cRate !== 0) {
          this.commentInfo = data.rate.list[0]
        }

        /* this.$nextTick(() => {
          //根据最新数据Dom已经渲染出来了,图片依然没有加载完
          // 目前获取offsetTop不包含图片
          this.themeTopYs.push(0);
          this.themeTopYs.push(this.$refs.params.$el.offsetTop);
          this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
          this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
          console.log(this.themeTopYs);
        }); */

      })

      /* 
        请求推荐数据
      */
      getRecommend().then(res => {
        // console.log(res);
        this.recommends = res.data.list
      })
    },
    /*  updated() {
 
       this.themeTopYs = []
 
       this.themeTopYs.push(0);
       this.themeTopYs.push(this.$refs.params.$el.offsetTop);
       this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
       this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
 
       console.log(this.themeTopYs);
     }, */
    mounted() {
    },
    /* 
      -详情页没有加入keep-live 所以不会调用actived和deactived等钩子函数
      -但是会调用destroyed生命周期函数
    */
    destroyed() {
      this.$bus.$off('itemImageLoad', this.itemImgListener)
    },

    methods: {
      imageLoad() {
        this.$refs.scroll.refresh()

        this.themeTopYs.push(0);
        this.themeTopYs.push(this.$refs.params.$el.offsetTop);
        this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
        this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
        // console.log(this.themeTopYs);

      },
      /* 
        头部导航点击处理函数
        翻滚到对应的主题
      */
      titleClick(index) {
        // console.log(index);
        this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 100)
      },
      /* 
        监听Scroll组件的滚动位置
      */
      contentScroll(position) {

        // 1 回到顶部组件是否显示
        this.isShowBackTop = -position.y > 1000

        const positionY = -position.y
        // 根据positionY和数组中的值对比,决定navbar中currentIndex为多少
        for (let i = this.themeTopYs.length - 1; i >= 0; i--) {
          if (positionY >= this.themeTopYs[i]) {
            this.$refs.navbar.currentIndex = i
            break;
          }
        }
      },
      /* 
        点击添加购物车处理函数
      */
      addToCart() {
        // 1 获取商品信息(购物车需要展示的信息)
        const product = {}
        product.image = this.topImages[0]
        product.title = this.goods.title
        product.desc = this.goods.desc
        product.price = this.goods.realPrice
        product.iid = this.iid

        // 2 将商品添加到购物车
        // this.$store.commit('addCart', product)
        this.$store.dispatch('addCart', product).then(res => {
          // 购物车添加成功的回调函数
          // this.show = true
          // this.message = res
          // setTimeout(() => {
          //   this.show = false
          //   this.message = ''
          // }, 1500);
          // console.log(this.$toast);
          this.$toast.show(res)
        })
      }
    },
    components: {
      DetailNavBar,
      DetailSwiper,
      DetailBaseInfo,
      DetailShopInfo,
      Scroll,
      DetailGoodsInfo,
      DetailParamInfo,
      DetailCommentInfo,
      GoodsList,
      DetailBottomBar,
      // Toast
    }
  }
</script>

<style scoped>
  #detail {
    position: relative;
    z-index: 9;
    background-color: #fff;
    height: 100vh;
  }

  .detail-nav {
    position: relative;
    z-index: 9;
    background-color: #fff;
  }

  .content {
    height: calc(100% - 44px - 58px);
  }
</style>